package com.ljs.clickhousedemo.util;


import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


/**
 * @Description:读取文件的字符集
 * @Author: liujinsheng
 * @Date: 2021/10/26.
 */
public class TestZip {
    /**
     * 判断文本文件的字符集，文件开头三个字节表明编码格式。
     * <a href="http://blog.163.com/wf_shunqiziran/blog/static/176307209201258102217810/">参考的博客地址</a>
     *
     * @param path
     * @return
     * @throws Exception
     * @throws Exception
     */
    public static String charset(String path) {
        String charset = "GBK";
        byte[] first3Bytes = new byte[3];
        try {
            boolean checked = false;
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
            bis.mark(0); // 读者注： bis.mark(0);修改为 bis.mark(100);我用过这段代码，需要修改上面标出的地方。
            // Wagsn注：不过暂时使用正常，遂不改之
            int read = bis.read(first3Bytes, 0, 3);
            if (read == -1) {
                bis.close();
                return charset; // 文件编码为 ANSI
            } else if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
                charset = "UTF-16LE"; // 文件编码为 Unicode
                checked = true;
            } else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF) {
                charset = "UTF-16BE"; // 文件编码为 Unicode big endian
                checked = true;
            } else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB
                    && first3Bytes[2] == (byte) 0xBF) {
                charset = "UTF-8"; // 文件编码为 UTF-8
                checked = true;
            }
            bis.reset();
            if (!checked) {
                while ((read = bis.read()) != -1) {
                    if (read >= 0xF0) {
                        break;
                    }
                    if (0x80 <= read && read <= 0xBF) // 单独出现BF以下的，也算是GBK
                    {
                        break;
                    }
                    if (0xC0 <= read && read <= 0xDF) {
                        read = bis.read();
                        if (0x80 <= read && read <= 0xBF) // 双字节 (0xC0 - 0xDF)
                            // (0x80 - 0xBF),也可能在GB编码内
                        {
                            continue;
                        } else
                            break;
                    } else if (0xE0 <= read && read <= 0xEF) { // 也有可能出错，但是几率较小
                        read = bis.read();
                        if (0x80 <= read && read <= 0xBF) {
                            read = bis.read();
                            if (0x80 <= read && read <= 0xBF) {
                                charset = "UTF-8";
                                break;
                            } else {
                                break;
                            }
                        } else {
                            break;
                        }
                    }
                }
            }
            bis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("--文件-> [" + path + "] 采用的字符集为: [" + charset + "]");
        return charset;
    }
    //将文件编码改为UTF-8
    private static void transferFile(String srcFileName, String destFileName) throws IOException {
        String line_separator = System.getProperty("line.separator");
        FileInputStream fis = new FileInputStream(srcFileName);
        StringBuffer content = new StringBuffer();
        DataInputStream in = new DataInputStream(fis);
        BufferedReader d = new BufferedReader(new InputStreamReader(in, "GBK"));// , "UTF-8"
        String line = null;
        while ((line = d.readLine()) != null) {
            content.append(line + line_separator);
        }
        d.close();
        in.close();
        fis.close();

        Writer ow = new OutputStreamWriter(new FileOutputStream(destFileName), "utf-8");
        ow.write(content.toString());
        ow.close();
    }

    /**
     *方法描述：<b>将多个文件压缩成zip包</b></br>
     */
    public ByteArrayOutputStream fileToZip(List<String>fileList,String zipName,String tempFilePath){
        byte[]buffer=new byte[1024];
        ZipOutputStream out=null;
        try{
            out=new ZipOutputStream(new FileOutputStream(tempFilePath+zipName+".zip"));
            List<File>filedata=new ArrayList<File>();
            for(int i=0,len=fileList.size();i<len;i++)
            {
                filedata.add(new File(fileList.get(i)));
            }

            for(int j=0,len=filedata.size();j<len;j++)
            {
                FileInputStream fis=new FileInputStream(filedata.get(j));
                out.putNextEntry(new ZipEntry(filedata.get(j).getName()));
                int dataLen;
//读入需要下载的文件的内容，打包到zip文件
                while((dataLen=fis.read(buffer))>0){
                    out.write(buffer,0,dataLen);

                }
                out.closeEntry();
                fis.close();

            }
            out.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
//读取压缩包
        File filezip=new File(tempFilePath+zipName+".zip");

        ByteArrayOutputStream baos=null;
        try
        {
            baos=new ByteArrayOutputStream();
            FileInputStream inStream=new FileInputStream(filezip);
            BufferedInputStream bis=new BufferedInputStream(inStream);
            int c=bis.read();
            while(c!=-1){
                baos.write(c);
                c=bis.read();
            }
            bis.close();
            inStream.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return baos;
        }


    public static void main(String[] args) throws Exception {
    String pastPath = "D:\\myitem\\click-house-demo\\tab_intensive1.zip";
    String nowPath="D:\\myitem\\click-house-demo\\tab_intensiveutf-8.zip";
    TestZip.charset(pastPath);
//        TestZip.charset(nowPath);
//        TestZip.transferFile(pastPath,nowPath);
    }


}
